﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Property;
using System.Drawing;
using System.Reflection;
using System.IO;
using System.Drawing.Imaging;

namespace HomeDMSObjectModel
{
    public class ImageAccessor : IPropertyAccessor
    {
        private ImageGetter _getter;
        private ImageSetter _setter;

        #region IPropertyAccessor Members

        public IGetter GetGetter(Type theClass, string propertyName)
        {
            return new ImageGetter(theClass, propertyName);
        }

        public ISetter GetSetter(Type theClass, string propertyName)
        {
            return new ImageSetter(theClass, propertyName);
        }

        #endregion

        internal class ImageGetter : IGetter
        {
            private string _propertyName;
            private Type _clazz;

            public ImageGetter(Type clazz,string propName)
            {
                _propertyName = propName;
                _clazz = clazz;
            }
            
            #region IGetter Members

            public object Get(object target)
            {
                PropertyInfo prop = _clazz.GetProperty(_propertyName);
                Image value = (Image)prop.GetValue(target, new object[] { });
                if (value != null)
                {
                    MemoryStream ms = new MemoryStream();
                    value.Save(ms, value.RawFormat);
                    return ms.GetBuffer();
                }
                else
                {
                    return null;
                }
            }

            public System.Reflection.MethodInfo Method
            {
                get
                {
                    PropertyInfo prop = _clazz.GetProperty(_propertyName);
                    if (prop != null)
                    {
                        return prop.GetGetMethod();
                    }
                    else
                    {
                        return null;
                    }
                }
            }

            public string PropertyName
            {
                get { return _propertyName; }
            }

            public Type ReturnType
            {
                get { return typeof(Image); }
            }

            #endregion
        }

        internal class ImageSetter : ISetter
        {
            private string _propertyName;
            private Type _clazz;

            public ImageSetter(Type clazz, string propName)
            {
                _propertyName = propName;
                _clazz = clazz;
            }
            
            #region ISetter Members

            public System.Reflection.MethodInfo Method
            {
                get
                {
                    PropertyInfo prop = _clazz.GetProperty(_propertyName);
                    if (prop != null)
                        return prop.GetSetMethod();
                    else
                        return null;
                }
            }

            public string PropertyName
            {
                get { return _propertyName; }
            }

            public void Set(object target, object value)
            {
                if (target != null && value != null)
                {
                    Byte[] arr = (Byte[])value;
                    Image img = new Bitmap(new MemoryStream(arr));
                    PropertyInfo prop = _clazz.GetProperty(_propertyName);
                    prop.SetValue(target, img, new object[] { });
                }
            }

            #endregion
        }


    }
}
